package com.briup.smartcity_group7.web.controller;

import com.briup.smartcity_group7.bean.basic.Category;
import com.briup.smartcity_group7.service.ICategoryService;
import com.briup.smartcity_group7.util.Result;
import com.briup.smartcity_group7.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "资讯栏目模块")
@RequestMapping("/auth/category")
public class CategoryController {
    @Autowired
    private ICategoryService service;

    @GetMapping("/page")
    public Result findByPage(Integer pageNum,Integer pageSize){
        PageInfo<Category> pageInfo = service.pageQueryAll(pageNum,pageSize);
        return ResultUtil.success(pageInfo);
    }

    @DeleteMapping
    public Result deleteById(@RequestParam(required = true) Integer id){
        System.out.println();
        service.deleteById(id);
        return ResultUtil.success("删除成功");
    }

    @DeleteMapping("/batch")
    public Result deleteManyById(Integer[] categoryIds){
        for(Integer id : categoryIds){
            service.deleteById(id);
        }
        return ResultUtil.success("删除成功");
    }

    @PostMapping
    public Result addCategory(Category category){
        service.saveCategory(category);
        return ResultUtil.success("新增成功");
    }

    @PutMapping
    public Result updateCategory(Category category){
        service.updateCategory(category);
        return ResultUtil.success("更新成功");
    }

    @GetMapping("/all")
    public Result findAll(){
        List<Category> all = service.findAll();
        return ResultUtil.success(all);
    }
}
